package pl.softech.gpw.dao;

import java.util.logging.Logger;

import pl.softech.gpw.dao.FileDownloadMonitor.FileDownloadEvent;
import pl.softech.gpw.dao.FileDownloadMonitor.FileDownloadListener;
import pl.softech.gpw.dao.FileDownloadMonitor.FileDownloadEvent.Type;

public class SimeFileDownloadListener implements FileDownloadListener {

	private static final long SIZE = 1024*1024;
	
	protected final Logger logger = Logger
			.getLogger(SimeFileDownloadListener.class.getName());

	private long bytesRecieved = 0;

	private long last = 0;
	
	private long size;

	@Override
	public void actionPerformed(FileDownloadEvent event) {

		if (event.type == Type.START) {
			bytesRecieved = 0;
			size = event.size;
		}

		if (event.type == Type.MESSAGE) {
			logger.info(event.message);
		}

		if (event.type == Type.BYTES_RECIEVED) {
			bytesRecieved += event.bytes;

			if (bytesRecieved / SIZE  > last) {
				last = bytesRecieved / SIZE;
				logger.info("Downloaded: " + bytesRecieved + "/" + size + " [" + ((float)bytesRecieved/size * 100) + "%]");

			}

		}

	}

	public static FileDownloadListener create() {
		return new SimeFileDownloadListener();
	}

}
